// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	1/6/2015 12:44:24 PM				
// -----------------------------

#include "DelayPass.h"

#include "FadePass.h"

DelayPass::DelayPass()
{
}

DelayPass::~DelayPass()
{
}

SoundSample DelayPass::Apply(const SoundSample& input) const
{
	SoundSample delaySample = {};
	if (UseDecay)
	{
		UInt32 sampleDecay = (UInt32)((Decay * input.SampleRate) * input.Channels);
		sampleDecay = Math::Min(sampleDecay, input.Count);
		Float* lpSamples = new Float[sampleDecay];
		memset(lpSamples, 0, sampleDecay * sizeof(Float));
		Float dv = DecayStart / (sampleDecay / input.Channels);
		for (UInt32 i = 0; i < (sampleDecay / input.Channels); ++i)
		{
			Float v = DecayStart - (i * dv);
			for (UInt32 c = 0; c < input.Channels; ++c)
				lpSamples[(i * input.Channels) + c] = input.Samples[(i * input.Channels) + c] * v;
		}

		delaySample.Channels = input.Channels;
		delaySample.SampleRate = input.SampleRate;
		delaySample.Samples = lpSamples;
		delaySample.Count = sampleDecay;
	}

	SoundSample output = input;
	UInt32 sampleOffset = (UInt32)((DelayOffset * input.SampleRate) * input.Channels);
	output.Merge(delaySample, sampleOffset);

	return output;
}